package com.wulian.common.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by macro on 2018/5/17.
 */
@Configuration
@ConditionalOnProperty(prefix = "aliyun", name = "oss.access-key-id")
public class OssConfig {
	@Bean
    @ConfigurationProperties(prefix = "aliyun.oss")
    public OssProperteis ossProperteis() {
        return new OssProperteis();
    }

    /**
     * oss客户端sdk
     *
     * @author fengshuonan
     * @Date 2018/6/27 下午6:10
     */
    @Bean
    public OSSClient ossClient(OssProperteis ossProperteis) {
        DefaultCredentialProvider defaultCredentialProvider =
                new DefaultCredentialProvider(ossProperteis.getAccessKeyId(), ossProperteis.getAccessKeySecret());
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        return new OSSClient(ossProperteis.getEndpoint(), defaultCredentialProvider, clientConfiguration);
    }
    
    @Bean
    public IAcsClient acsClient(OssProperteis ossProperteis) {
		IClientProfile profile = DefaultProfile.getProfile(ossProperteis.getArea(), ossProperteis.getAccessKeyId(),
				ossProperteis.getAccessKeySecret());
		try {
			DefaultProfile.addEndpoint(ossProperteis.getArea(), ossProperteis.getArea(), ossProperteis.getProduct(),
					ossProperteis.getDomain());
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        IAcsClient acsClient = new DefaultAcsClient(profile);
        return acsClient;
    }
}
